﻿#pragma once
#include "MST.h"
#include "Grid.h"
#include "RoutingNode.h"
#include "../src_slide/slide.h"

class RouterMeshless {
public:
	RouterMeshless() :m_netTrees(nullptr) {};
	RouterMeshless
	(
		std::unordered_map<std::string, std::shared_ptr<SteinerNode>>* netTrees,
		std::vector<PinPolygon>* obss,
		std::vector<double>* bound,
		double gridSize = 10
	)
		: m_netTrees(netTrees), m_obss(obss), m_bound(*bound)
	{
		initializeGrid(gridSize);   // 初始化网格
	};
	~RouterMeshless() {
		// 释放内存std::unordered_map<std::string, std::vector<PathNode*>> m_pathNodes;
		for (auto& oneNetPaths : m_pathNodes) {
			for (auto headNode : oneNetPaths.second) {
				PathNode* current = headNode;
				while (current) {
					PathNode* next = current->next;
					delete current;
					current = next;
				}
			}
		}
	};

	void run();
	std::unordered_map<std::string, std::vector<PathNode*>>& getPathNodes() { return m_pathNodes; };

private:
	std::unordered_map<std::string, std::shared_ptr<SteinerNode>>* m_netTrees;
	std::vector<PinPolygon>* m_obss;
	std::vector<double> m_bound;
	std::unique_ptr<GridManager> m_gridManager;

	std::unordered_map<std::string, std::vector<PathNode*>> m_pathNodes;

	std::vector<PathNode*> m_temp;  // 环形链表的头节点集合，用于内存管理

	std::function<bool(const std::pair<PinPolygon*, PinPolygon*>&, const std::pair<PinPolygon*, PinPolygon*>&)> m_priorityRule;


private:
	void initializeGrid(double& gridSize);      // 初始化网格
	std::vector<std::pair<PinPolygon*, PinPolygon*>> extractPinPairs(std::shared_ptr<SteinerNode> steinerTree);
	// 辅助函数
	void getCandidatePolygons(const PathNode* node_start, const PathNode* node_end, const std::vector<GridCell*>& cells, std::unordered_set<PinPolygon*>& obss)const;
	bool getSortedNodes(const Line& line, std::unordered_set<PinPolygon*>& obss,std::vector<PathNode*>& crossingNodes);
	bool getLinePolygonIntersections(const Line& line, PinPolygon* poly, std::vector<PathNode*>& crossingNodes);
	void lineHugPolygons(PathNode* node_start, PathNode* node_end, std::vector<PathNode*>& crossingNodes);
	bool changeTwoNodesConnection(PathNode* start, PathNode* end, PathNode* n11, PathNode* n21);
	void slideNodeLines(PathNode* node_start, PathNode* node_end);

	// 默认优先级规则：按距离排序（距离短的优先）
	static bool defaultPriorityRule(const std::pair<PinPolygon*, PinPolygon*>& a,
		const std::pair<PinPolygon*, PinPolygon*>& b) {
		double distA = a.first->m_center.distanceTo(a.second->m_center);
		double distB = b.first->m_center.distanceTo(b.second->m_center);
		return distA < distB;
	}
};


/*
	bool getSortedObss(const Line& line, std::vector<PinPolygon*>& obss, std::vector<PathNode*>& minMaxNodes);
	std::pair<double, double> setMinMaxNode(const Line& line, std::vector<PathNode*>& crossingNodes, PathNode* minNode, PathNode* maxNode);
	void lineHugPolygon(const std::vector<PinPolygon*>& obss, PathNode* node_start, PathNode* node_end);

	bool processSingleObstacle(PathNode* start, PathNode* end, PinPolygon* obstacle);

	bool changeNodesConnection(PathNode* start, PathNode* end, std::vector<PathNode*>& crossingNodes);
	//
	*/

